//
//  YTileLayer.m
//  yuimall_Iphone
//
//  Created by Gang.Wang on 12-4-26.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "YTileLayer.h"
#import <QuartzCore/QuartzCore.h>
#import "header.h"


@implementation YTileLayer
@synthesize tileIndex;
@synthesize tileName;

@synthesize editStatus;


-(void) dealloc
{
    YRelease(tileName);
    YRelease(closeLayer);
    
    YRelease(titleLayer);
    YRelease(contentLayer);
    
    [super dealloc];
}

- (void) createCloseLayer
{
    CALayerEx * close = [CALayerEx layer];
    closeLayer = [close retain];
    //        [closeLayer setBackgroundColor:COLOR_RED.CGColor];
    closeLayer.layerId = [NSString stringWithFormat:@"%@/close", self.layerId];
    [self addSublayer:closeLayer];
}

- (void) createContentLayers
{
    CALayerEx * title_ = [CALayerEx layer];
    titleLayer = [title_ retain];
    
    [self addSublayer:self->titleLayer];
    
    CALayerEx * content__ = [CALayerEx layer];
    contentLayer = [content__ retain];
    
    [self addSublayer:self->contentLayer];
    
    
    [self createCloseLayer];
}

- (void) setEditStatus:(BOOL)editStatusValue
{
    editStatus = editStatusValue;
    [closeLayer setHidden:!editStatusValue];  
//    if (editStatus) {
//        [self startWiggling];
//    }
//    else {
//        [self stopWiggling];
//    }
}

- (id)init {
    self = [super init];
    if (self) 
    {
        self.name = [NSString stringWithString:_YTILELAYER_NAME_];
        
        [self createContentLayers];   
        [closeLayer setHidden:YES];
        self.editStatus = FALSE;
    }
    return self;
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [closeLayer setFrame:CGRectMake(0, 0, 20, 20)];
}



- (void)draw 
{
    NSString *labelText = [NSString stringWithFormat:@"%d", (int)tileIndex];
    
    UIFont *font = [UIFont boldSystemFontOfSize:36];
    [[UIColor purpleColor] set];    
     CGRect rect = CGRectMake(0, (int)self.bounds.size.height - 20, (int)self.bounds.size.width, 20);
    [labelText drawCenteredInRect:rect withFont:font];
}


- (void)appearDraggable {
    self.opacity = 0.6;
    [self setValue:[NSNumber numberWithFloat:1.25] forKeyPath:@"transform.scale"];
}


- (void)appearNormal {
    self.opacity = 1.0;
    [self setValue:[NSNumber numberWithFloat:1.0] forKeyPath:@"transform.scale"];
}


- (void)startWiggling {
    
    self.editStatus = TRUE;
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    anim.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:-0.05],
                   [NSNumber numberWithFloat:0.05],
                   nil];
    anim.duration = 0.09f + ((tileIndex % 10) * 0.01f);
    anim.autoreverses = YES;
    anim.repeatCount = HUGE_VALF;
    [self addAnimation:anim forKey:@"wiggleRotation"];
    
    anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
    anim.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:-1],
                   [NSNumber numberWithFloat:1],
                   nil];
    anim.duration = 0.08f + ((tileIndex % 10) * 0.01f);
    anim.autoreverses = YES;
    anim.repeatCount = HUGE_VALF;
    anim.additive = YES;
    [self addAnimation:anim forKey:@"wiggleTranslationY"];
}


- (void)stopWiggling {
    self.editStatus = FALSE;
    
    [self removeAnimationForKey:@"wiggleRotation"];
    [self removeAnimationForKey:@"wiggleTranslationY"];
}

@end
